//
//  ViewController.m
//  UI_17_homework_Preview_NSFileManage
//
//  Created by li cheng on 16/8/5.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *imageName;
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *contactWay;
@property (weak, nonatomic) IBOutlet UITextField *address;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    [self NSFileMagr];
    
   
}

- (void)NSFileMagr{
    // 错误信息
    NSError *error;
    // 创建文件管理器
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    // 指向文件目录
    // NSHomeDirectory 返回用户或应用程序的主目录
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    // 创建新的文件夹
    NSString *testDirectory = [documentsDirectory stringByAppendingPathComponent:@"testbug1"];
    // 创建一个目录
    // createDirectoryAtPath 创建对应的目录在相应的路径
    // withIntermediateDirectories 这是一个BOOL值,加入是YES,路径当中的父类路径不存在,也一并创建了,加入是NO,那么久不创建,方法失效,还有就是创建的地方是一个文件夹没有目录,这个方法也失效.
    [fileMgr createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    //    NSLog(@"%@", testDirectory);
    // 2.创建文件
    NSString *test11 = [testDirectory stringByAppendingPathComponent:@"test111.txt"];
    NSString *test22 = [testDirectory stringByAppendingPathComponent:@"test222.txt"];
    NSString *string = @"写入内容1";
    [fileMgr createFileAtPath:test11 contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    [fileMgr createFileAtPath:test22 contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    // 3.查看Document里的所有文件
    NSArray *files = [fileMgr subpathsAtPath:documentsDirectory];
    //    NSLog(@"最后一个文件是%@", [files lastObject]);
    
    // 4. 查看目录里所有子文件
    NSArray *subfiles = [fileMgr subpathsAtPath:testDirectory];
    //    NSLog(@"子文件%@", subfiles);
    //    NSLog(@"%@", files);
    // 5.更改到待操作的目录下
    [fileMgr changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
    // 创建文件filename文件名, contents文件内容
    //    NSString *fileName = @"testfilemanager.txt";
    //    NSArray *array = [[NSArray alloc] initWithObjects:@"1234", @"1234", nil];
    //    [fileMgr createFileAtPath:fileName contents:array attributes:nil];
    // 6.删除文件
    //    [fileMgr removeItemAtPath:test11 error:nil];
    // 复制文件后者给前者
    NSString *str = [documentsDirectory stringByAppendingPathComponent:@"DDD"];
    NSError *error1;
    [fileMgr copyItemAtPath:test11 toPath:testDirectory error:&error1];
    // 8.移动文件
    //    NSString *test33 = [documentsDirectory stringByAppendingPathComponent:@"test33.txt"];
    //    if ([fileMgr moveItemAtPath:test11 toPath:test33 error:nil]) {
    //        NSLog(@"111%@", [error1 localizedDescription]);
    //    }
}

- (IBAction)completeAdd:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(imageName:name:contantWay:address:)]) {
        [self.delegate imageName:_imageName.text name:_name.text contantWay:_contactWay.text address:_address.text];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
